/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.compile;

import com.inspur.edp.sgf.api.entity.ServiceOperation;
import com.inspur.edp.sgf.api.entity.SgMetadata;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.Objects;

/**
 * @author Fynn Qi
 * @author Kaixuan Shi
 * @version 1.1
 * @since 2020/9/17 18:18
 */
public class EapiChecker {

    public static void check(SgMetadata metadata) {
        if (metadata == null) {
            throw new RuntimeException("外部服务元数据代码生成校验异常：外部服务元数据为空，请检查入参");
        }
        if (metadata.getService() == null) {
            throw new RuntimeException(MessageFormat.format("外部服务定义[{0}](ID:{1})中服务定义不能为空", metadata.getName(), metadata.getId()));
        }
        if (StringUtils.isBlank(metadata.getCriticalApplication())) {
            throw new RuntimeException(MessageFormat.format("外部服务定义[{0}](ID:{1})中关键应用不能为空", metadata.getName(), metadata.getId()));
        }
        if (StringUtils.isBlank(metadata.getServiceUnit())) {
            throw new RuntimeException(MessageFormat.format("外部服务定义[{0}](ID:{1})中服务单元不能为空", metadata.getName(), metadata.getId()));
        }
        if (StringUtils.isBlank(metadata.getVersion())) {
            throw new RuntimeException(MessageFormat.format("外部服务定义[{0}](ID:{1})中服务版本不能为空", metadata.getName(), metadata.getId()));
        }

        if (StringUtils.isBlank(metadata.getService().getResourceType())) {
            throw new RuntimeException(MessageFormat.format("外部服务定义[{0}](ID:{1})中资源类型不能为空", metadata.getName(), metadata.getId()));
        }

        if (StringUtils.isBlank(metadata.getService().getResourceId())) {
            throw new RuntimeException(MessageFormat.format("外部服务定义[{0}](ID:{1})中资源ID不能为空", metadata.getName(), metadata.getId()));
        }

        if (metadata.getService().getOperations() == null || metadata.getService().getOperations().isEmpty()) {
            throw new RuntimeException(MessageFormat.format("外部服务定义[{0}](ID:{1})中服务列表不能为空", metadata.getName(), metadata.getId()));
        }

        if (Objects.nonNull(metadata.getService().getOperations())) {
            for (ServiceOperation operation : metadata.getService().getOperations()) {
                if (StringUtils.isBlank(operation.getHttpMethod())) {
                    throw new RuntimeException(
                            MessageFormat.format(
                                    "外部服务定义[{0}](ID:{1})中服务操作[{2}]的HTTP方法不能为空", metadata.getName(), metadata.getId(), operation.getName()));
                }
            }
        }
    }
}
